<template>
  <div v-show="$route.name == 'house-list'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>仓库列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="1.5">
          <el-button type="success" @click="goAddHouse()" v-if="role == 1 || role == 3">采购新品</el-button>
        </el-col>
      </el-row>

      <!-- 列表区域 -->
      <el-table
        :data="list"
        :span-method="(...arg)=>strategySpanMethod(...arg,list)"
        border
        v-loading="loading"
        style="width: 100%; margin-top: 20px">
        <el-table-column min-width="120" prop="product" label="类型" fixed="left"></el-table-column>
        <el-table-column min-width="160" prop="productn" label="产品名称" fixed="left"></el-table-column>
        <el-table-column min-width="160" prop="model" label="规格" fixed="left"></el-table-column>
        <el-table-column width="110" prop="unit" label="单位"></el-table-column>
        <el-table-column min-width="110" prop="amoumt" label="可销售库存"></el-table-column>
        <el-table-column min-width="110" prop="wamoumt" label="待发货库存"></el-table-column>
        <el-table-column min-width="110" prop="oldamoumt" label="旧品库存"></el-table-column>
        <el-table-column min-width="90" prop="oldwamoumt" label="旧品待发货库存"></el-table-column>
        <el-table-column min-width="110" prop="badamoumt" label="损坏"></el-table-column>
        <el-table-column min-width="110" prop="nowamoumt" label="实际库存"></el-table-column>
         <el-table-column min-width="110" prop="show" label="在展数量"></el-table-column>
        <el-table-column min-width="110" prop="nowcost" label="库存成本"></el-table-column>
        <el-table-column min-width="110" prop="price" label="默认售价"></el-table-column>
        <el-table-column label="操作" width="160" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleSave(scope.$index, scope.row)"
              v-if="role == 1 || role == 3">采购</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalRecord">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      role: '',
      list: [],
      loading: true,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalRecord: 0
      }
    }
  },
  watch: {
  },
  created() {
    this.role = localStorage.getItem('role')
    this.getHouse()
  },
  methods: {
    // 新增按钮
    goAddHouse() {
      this.$router.push('/house-multi')
    },
    // 获取仓库列表
    async getHouse() {
      let params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize
      }
      const { data: result } = await this.$http.get('/house', { params: params })
      this.loading = false
      if (result.status !== 200) return this.$message.error(result.msg)
      this.list = result.data
      this.pager = result.pager
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getHouse()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(currPage) {
      this.pager.currPage = currPage
      this.getHouse()
    },
    // 处理合并行的数据
    getSpanArr: function(data, spanKey) {
      let spanArr = []
      let pos = ''
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          spanArr.push(1)
          pos = 0
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i][spanKey] === data[i - 1][spanKey]) {
            spanArr[pos] += 1
            spanArr.push(0)
          } else {
            spanArr.push(1)
            pos = i
          }
        }
      }
      return spanArr
    },
    strategySpanMethod: function({ row, column, rowIndex, columnIndex }, data) {
      // columnIndex判断第一列或第二列合并 列数从0开始
      if (columnIndex === 0 || columnIndex === 1) {
      // column.property是表头的key
        let spanArr = this.getSpanArr(data, column.property)
        const _row = spanArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    // 编辑
    handleEdit(index, row) {
      this.$router.push({
        name: 'house-update',
        params: { id: row.id }
      })
    },
    handleSave(index, row) {
      this.$router.push({
        name: 'house-save',
        params: { id: row.id }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.query {
  margin-bottom: 20px;
}
.el-select {
  width: 100%;
}
.el-date-editor {
  width: 100%;
}
</style>
